package com.juejin.www.service.impl;
import com.juejin.www.dao.*;
import com.juejin.www.model.DO.Audit;
import com.juejin.www.model.DO.BoilingPoint;
import com.juejin.www.model.DTO.BoilingPointDTO;
import com.juejin.www.model.DTO.PageDTO;
import com.juejin.www.model.DTO.UserDTO;
import com.juejin.www.model.VO.BoilingPointVO;
import com.juejin.www.service.BoilingPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jacky
 */
@Service
public class BoilingPointImpl implements BoilingPointService {

    @Autowired
    private BoilingPointDao boilingPointDao;

    @Autowired
    private UserDao userDao;
    
    @Autowired
    private AdminDao adminDao;

    @Autowired
    private LikeDao likeDao;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageDTO<BoilingPointVO> list(Integer begin, Integer uid) {
        List<BoilingPoint> boilingPoints = boilingPointDao.list(begin);
        List<BoilingPointVO> rows = boilingPoints.stream()
                .map(boilingPoint -> {
                    UserDTO author = userDao.getById(boilingPoint.getAuthorId());
                    Boolean isLiked=false;
                    if(uid==null){
                        isLiked=false;
                    }else if(likeDao.checkLike(boilingPoint.getId(),2,uid)>0){
                        isLiked=true;
                    }
                    return new BoilingPointVO(
                            boilingPoint.getId(),
                            boilingPoint.getContent(),
                            author.getName(),
                            boilingPoint.getAuthorId(),
                            boilingPoint.getCreateTime(),
                            boilingPoint.getLikeCount(),
                            boilingPoint.getCommentCount(),
                            isLiked
                    );
                })
                .collect(Collectors.toList());
        int totalCount = boilingPointDao.getTotalCount();
        PageDTO<BoilingPointVO> boilingPointVos = new PageDTO<>(totalCount, rows);
        return boilingPointVos;
    }

    @Override
    public void addBoilingPoint(BoilingPointDTO boilingPointDTO) {
        Audit audit = new Audit(null,
                null,
                boilingPointDTO.getContent(),
                2,
                boilingPointDTO.getAuthorId(),
                0, null, null, null,
                LocalDateTime.now(),
                null);
        adminDao.addAudit(audit);
    }

    @Override
    public void deleteBoilingPoint(Integer id) {
        boilingPointDao.deleteBoilingPoint(id);
        String commentKey = "comment:2:"+id;
        String commentCacheValue = stringRedisTemplate.opsForValue().get(commentKey);
        if(commentCacheValue!=null){
            stringRedisTemplate.delete(commentKey);
        }
        commentDao.deleteCommentWhenDeleteItem(id,2);
    }
}
